import type { Ref } from "vue";
import type { tablePageTy } from "./type";
// import { object } from '~/common';
import type { AxiosPromise } from "axios";
import { ElMessageBox, ElMessage } from "element-plus";
import { cloneDeep } from "lodash-unified";
export interface CudOptions {
  cRequest?: (...args) => AxiosPromise; // 新增
  uRequest?: (...args) => AxiosPromise; // 更新
  dRequest?: (...args) => AxiosPromise; // 删除
  tableRequest?: (page: Ref<tablePageTy>, params?: object) => void; // avue table onload 方法
  page: Ref<tablePageTy>;
  // 前、后置处理
  beforCReqFunc?: (row: any) => void;

  afterCReqFunc?: (row: any) => void;
  beforUReqFunc?: (row: any) => void;
  afterUReqFunc?: (row: any) => void;
  beforDReqFunc?: (row: any) => void;
  afterDReqFunc?: (row: any) => void;
  // 额外的请求参数
  cQuery?: object | Ref<object>;
  uQuery?: object | Ref<object>;
  // id - key
  rowIdKey?: string;
  // mock 配置
  isMock?: boolean;
  mockDelay?: number;
}
/**
 *
 * @param options 配置项
 * @returns
 */
export default function (options: CudOptions) {
  /*增、删、改*/
  const _ = options;
  const pageRow = unref(_?.page);
  const defaultPage = {
    pageSize: 10,
    currentPage: 1,
    total: 0,
  };
  // del
  const rowDel = (row: object) => {
    ElMessageBox.confirm("确定将选择数据删除?", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      if (_.isMock) {
        setTimeout(() => {
          ElMessage({
            type: "success",
            message: "操作成功!",
          });
        }, _.mockDelay ?? 1000);
      } else {
        const rowCopy = cloneDeep(row);
        _?.beforDReqFunc?.(rowCopy); // 请求自定义前处理
        // 删除请求
        try {
          await _?.dRequest?.(rowCopy[_?.rowIdKey ?? "id"]);
          _?.afterDReqFunc?.(rowCopy); // 请求自定义后处理
          ElMessage({
            type: "success",
            message: "操作成功!",
          });
          // 更新table数据
          await _?.tableRequest?.(_.page);
        } catch (error) {
          console.log("del error:", error);
        }
      }
    });
  };

  // create
  const rowSave = async (row, done, loading) => {
    if (_.isMock) {
      setTimeout(() => {
        ElMessage({
          type: "success",
          message: "操作成功!",
        });
      }, _.mockDelay ?? 1000);
    } else {
      const rowCopy = cloneDeep(row);
      _?.beforCReqFunc?.(rowCopy); // 请求自定义前处理
      try {
        loading();
        await _?.cRequest?.({ ...rowCopy, ...unref(_?.cQuery ?? {}) });
        done();
        ElMessage({
          type: "success",
          message: "操作成功!",
        });
        // 更新table数据
        await _?.tableRequest?.(_.page);
        _?.afterCReqFunc?.(rowCopy); // 请求自定义后处理
      } catch (error) {
        // done();
        console.log("create error:", error);
      }
    }
  };

  // update
  const rowUpdate = async (row, index, done, loading) => {
    if (_.isMock) {
      setTimeout(() => {
        ElMessage({
          type: "success",
          message: "操作成功!",
        });
      }, _.mockDelay ?? 1000);
    } else {
      const rowCopy = cloneDeep(row);
      _?.beforUReqFunc?.(rowCopy); // 请求自定义前处理
      try {
        loading();
        await _?.uRequest?.({ ...rowCopy, ...unref(_?.uQuery ?? {}) });
        done();
        ElMessage({
          type: "success",
          message: "操作成功!",
        });
        // 更新table数据
        await _?.tableRequest?.(_.page);
        _?.afterUReqFunc?.(rowCopy); // 请求自定义后处理
      } catch (error) {
        done();
        console.log("create error:", error);
      }
    }
  };

  return {
    rowDel,
    rowSave,
    rowUpdate,
  };
}
